﻿using GrapeCity.Forguncy.CellTypes;
using GrapeCity.Forguncy.Commands;
using GrapeCity.Forguncy.Plugin;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Vant.Properties;
using Vant.WpfControls.DrawingObject;


namespace Vant.CellTypes
{
    public class UploadDesigner : CellTypeDesigner<Upload>, ISupportPropertyInitialize
    {
        public override FrameworkElement GetDrawingControl(ICellInfo cellInfo, IDrawingHelper drawingHelper)
        {
            return new UploadDrawingObject(drawingHelper, this.CellType);
        }

        public void InitDefaultPropertyValues(IBuilderContext context)
        {
            this.CellType.accept = ".jpg,.jpeg,.png";
            this.CellType.capture = "images";
            this.CellType.previewSize = 80;
            this.CellType.imageFit = "cover";
            this.CellType.OverFileSizeLimitError = Resources.Upload_OverFileSizeLimitError;
        }
    }

    [Designer("Vant.CellTypes.UploadDesigner, Vant")]
    [Icon("pack://application:,,,/Vant;component/Resources/Images/Uploader.png")]
    [OrderWeight((int)Utils.OrderWeight.Upload)]
    public class Upload : VantCellTypeBase, ISupportUIPermission, ISupportDisable, ISupportReadOnly
    {
        List<UIPermission> _uIPermissions;
        [SRDisplayName(nameof(Resources.Vant_UIPermissions))]
        [JsonProperty(ObjectCreationHandling = ObjectCreationHandling.Replace)]
        public List<UIPermission> UIPermissions
        {
            get
            {
                if (_uIPermissions == null)
                {
                    _uIPermissions = Utils.GetDefaultPermission(UIPermissionScope.Enable, UIPermissionScope.Visible, UIPermissionScope.Editable);
                }
                return _uIPermissions;
            }
            set
            {
                _uIPermissions = value;
            }
        }

        [SRDisplayName(nameof(Resources.Uploader_MaxCount))]
        [SRIntProperty(Min = 1, AllowNull = true, Watermark = nameof(Resources.Vant_NotLimit))]
        public int? maxCount { get; set; }

        [SRDisplayName(nameof(Resources.Uploader_Accept))]
        public string accept { get; set; }

        double? _maxSize;
        [SRDisplayName(nameof(Resources.Uploader_MaxSize))]
        [SRDoubleProperty(Min = 0, AllowNull = true, Watermark = nameof(Resources.Vant_NotLimit))]
        public double? maxSize
        {
            get
            {
                return _maxSize;
            }
            set
            {
                if (value == 0)
                {
                    value = null;
                }
                _maxSize = value;
            }
        }

        [SRDisplayName(nameof(Resources.Uploader_Capture))]
        [SRComboProperty(ValueList = "images|camera", DisplayList = nameof(Resources.Uploader_Capture_DisplayList))]
        public string capture { get; set; }

        [SRDisplayName(nameof(Resources.Uploader_ShowUpload))]
        [BoolProperty]
        [DefaultValue(true)]
        public bool showUpload { get; set; } = true;

        [SRDisplayName(nameof(Resources.Uploader_UploadIcon))]
        [IconProperty]
        public ImageValue uploadIcon { get; set; }

        [SRDisplayName(nameof(Resources.Uploader_UploadText))]
        public string uploadText { get; set; }

        [SRDisplayName(nameof(Resources.Uploader_ImageLimit))]
        [UploadImageLimitSetting]
        public object UploadImageLimit { get; set; }

        [SRDisplayName(nameof(Resources.Uploader_PreviewSize))]
        [IntProperty(Min = 1)]
        public int previewSize { get; set; }

        [SRDisplayName(nameof(Resources.Uploader_ImageFit))]
        [SRComboProperty(ValueList = "fill|contain|cover|none|scale-down", DisplayList = nameof(Resources.Uploader_ImageFit_DisplayList))]
        public string imageFit { get; set; }

        [SRCategoryHeader(nameof(Resources.Other))]
        [SRDisplayName(nameof(Resources.Uploader_PreviewFullImage))]
        [DefaultValue(true)]
        public bool previewFullImage { get; set; } = true;

        [SRDisplayName(nameof(Resources.Uploader_Deletable))]
        [DefaultValue(true)]
        public bool deletable { get; set; } = true;

        [SRDisplayName(nameof(Resources.Uploader_Multiple))]
        [DefaultValue(false)]
        public bool multiple { get; set; } = false;

        [SRDisplayName(nameof(Resources.Vant_Disable))]
        [DefaultValue(false)]
        public bool IsDisabled { get; set; } = false;

        [SRDisplayName(nameof(Resources.Vant_Readonly))]
        [DefaultValue(false)]
        public bool ReadOnly { get; set; } = false;

        [Browsable(false)]
        [SaveJsonIgnore]
        [ServerProperty]
        public UploadLimit UploadLimit
        {
            get
            {
                return new UploadLimit()
                {
                    ExtensionFilter = this.accept,
                    SizeLimit = this.maxSize ?? 0
                };
            }
        }

        [Browsable(false)]
        public string OverFileSizeLimitError { get; set; }

        [Browsable(false)]
        public string UnacceptableFileError
        {
            get
            {
                return Resources.Upload_UnacceptableFileError;
            }
        }

        public override SupportFeatures SupportFeatures
        {
            get
            {
                return SupportFeatures.ShouldCheckDirtyWhenLeavePage;
            }
        }

        public override bool GetDesignerPropertyVisible(string propertyName)
        {
            if (propertyName == nameof(uploadIcon) || propertyName == nameof(uploadText) || propertyName == nameof(UploadImageLimit))
            {
                return this.showUpload;
            }
            if (propertyName == nameof(multiple))
            {
                return this.capture == "images";
            }
            return base.GetDesignerPropertyVisible(propertyName);
        }

        public override string ToString()
        {
            return Resources.Uploader_DisplayName;
        }
    }

    public class UploadLimit : IUploadLimit
    {
        public UploadLimit()
        {
            ExtensionFilter = "";
            SizeLimit = 0;
        }
        [DefaultValue("")]
        public string ExtensionFilter { get; set; }

        [DefaultValue(0d)]
        public double SizeLimit { get; set; }
    }
}
